package com.boyon.system.action.validator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;

import com.boyon.common.domain.UserSession;
import com.boyon.common.status.AccountType;
import com.boyon.common.status.Constant;
import com.boyon.system.entity.Account;
import com.boyon.web.param.domain.FieldError;
import com.boyon.web.param.domain.Result;

/**
 * ClassName:AccountValidator <br/>
 * Function: 账号操作验证类. <br/>
 * Date: 2018年3月21日 上午11:06:02 <br/>
 * 
 * @author sunshulin
 * @version
 * @since JDK 1.8
 * @see
 */
@Component
public class AccountValidator {

	/**
	 * 
	 * create:创建账号验证. <br/>
	 * 
	 * @author sunshulin
	 * @param param
	 * @param result
	 * @param request
	 * @param session
	 * @return
	 * @since JDK 1.8
	 */
	public boolean create(Account param, Result result,
			HttpServletRequest request, HttpSession session) {
		UserSession userSession = (UserSession) session
				.getAttribute(Constant.SESSION_USER);
		AccountType type = AccountType.getType(userSession.getAccountType());
		AccountType paramType = AccountType.getType(param.getAccountType());
		switch (type) {
			case system:
				if (paramType != AccountType.manager) {
					result.addError(
							new FieldError("accountType", "系统账号只能添加管理账号", ""));
				}
				break;
			case manager:
				if (paramType != AccountType.user) {
					result.addError(new FieldError("accountType",
							"管理账号只能添加用户账号和管理账号", ""));
				}
				break;
			case user:
				result.addError(
						new FieldError("accountType", "用户账号不能添加账号", ""));
				break;
			default:
				result.addError(new FieldError("accountType", "未知账号类型", ""));
		}
		return result.isError();
	}
}
